module player {
    export abstract class Player extends egret.DisplayObjectContainer {
        private pic: egret.Bitmap;

        private direction: Direction;
        private speed: number;

        private runNum: number = 0;

        private realX: number;
        private realY: number;

        constructor() {
            super();
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        }

        private onAddToStage() {
            this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
            let prefix = this.picPrefix();
            this.pic = BitmapUtils.createBitmapByName(prefix + "." + prefix + 1);
            this.addChild(this.pic);
        }

        public abstract picPrefix(): string;

        public abstract picDirection(): any;

        /**
         * 闲置状态
         */
        public idle(direction: Direction = Direction.SOUTH) {
            if (this.isMove()) {
                this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
            }
            let name = DirectionUtils.getNameWithLowercase(direction);
            if (this.direction) {
                name = DirectionUtils.getNameWithLowercase(this.direction);
            }
            let groupNo = this.picDirection()[name];
            let picNum = (groupNo - 1) * 8 + 1;
            BitmapUtils.changeTexture(this.pic, this.picPrefix() + "." + this.picPrefix() + picNum);
            this.runNum = 0;
        }

        /**
         * 朝指定方向，以固定速度移动
         */
        public onMove(e: MoveEvent) {
            this.direction = e.direction;
            this.speed = e.speed;
            if (this.isIdle()) {
                this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
            }
        }

        private onEnterFrame() {
            // 跑起来
            let name = DirectionUtils.getNameWithLowercase(this.direction);
            let groupNo = this.picDirection()[name];
            let picNum = (groupNo - 1) * 8 + this.runNum++ % 8 + 1;
            BitmapUtils.changeTexture(this.pic, this.picPrefix() + "." + this.picPrefix() + picNum)
        }

        private isMove(): boolean {
            return this.hasEventListener(egret.Event.ENTER_FRAME);
        }

        private isIdle(): boolean {
            return !this.hasEventListener(egret.Event.ENTER_FRAME);
        }

        /**
         * 用于对角色进行定位，例如：游戏初始化时、瞬间传送等场景。
         */
        public locate(x: number, y: number) {

        }


    }
}